﻿using IBaseRepository;
using SqlSugarAndEntity.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core;
using Services.Base;
using Common.ExpressionHelper;
using Common.CommonModel;
using AutoRegistDependency.Attributes;

namespace Services.Menu
{
    [Component]
    public class MenuService:BaseService<sys_menuinfo>,IMenuService
    {
        private IMenuRepository menuRepository;
        public MenuService(IMenuRepository menuRepository) : base(menuRepository)
        {
            this.menuRepository = menuRepository;
        }

        public ResponseModel GetLazyMenuTreeNode(decimal? ParentMenuID,int page,int limit)
        {
            return CreateResponseModel(menuRepository.GetLazyMenuTreeNode, ParentMenuID,page,limit);
        }

        public ResponseModel GetMenuByRoleId(decimal roleId)
        {
            return CreateResponseModel(menuRepository.GetMenuByRoleId, roleId);
        }

        public ResponseModel GetMenuByUserId(decimal userId)
        {
            return CreateResponseModel(menuRepository.GetMenuByUserId, userId);
        }

        public ResponseModel GetMenuTree()
        {
            ResponseModel response = new ResponseModel();
            DataTable dt = menuRepository.GetMenuTree();
            //response.model = ReflectionConvertHelper.ConvertDatatableToTreeList<sys_menuinfo>(dt, "ID", "MenuParentID");
            response.model= ExpressionHelper.ConvertToTreeList(menuRepository.GetInfo(t => true), t => t.MenuParentID == 0 || t.MenuParentID == null, t => t.ID ==t.MenuParentID );
            return response;
        }
    }
}
